package com.qjc.demo.train.divide;

/**
 * @ClassName: PowDemo
 * @Description: 分治算法-计算x的n次幂
 * @Author: qjc
 * @Date: 2022/5/4 11:25 AM
 */
public class PowDemo {

    public static int dividePow(int x, int n) {
        if (n == 1) return x;
        int half = dividePow(x, n / 2);
        if (n % 2 == 0) {
            return half * half;
        } else {
            return half * half * x;
        }
    }

    public static void main(String[] args) {
        System.out.println(dividePow(2, 10));
    }
}
